package com.jinzhi.jzweb.controller;


import java.text.ParseException;
import java.util.*;

import com.jinzhi.jzweb.domain.vo.JazGroupProdVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazProdTypeDO;
import com.jinzhi.jzweb.service.JazProdTypeService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 商品类型表
 * </pre>
 * <small> 2024-07-30 16:36:30 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazProdType")
public class JazProdTypeController extends BaseController {
	@Autowired
	private JazProdTypeService jazProdTypeService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazProdType:jazProdType")
	String JazProdType(){
	    return "jzweb/jazProdType/jazProdType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<JazProdTypeDO>> list(JazProdTypeDO jazProdTypeDTO){
		// 查询列表数据
		Page<JazProdTypeDO> page = jazProdTypeService.selectPage(getPage(JazProdTypeDO.class),
					jazProdTypeService.convertToEntityWrapper()
							.like("name","%"+jazProdTypeDTO.getName()+"%")
							.orderBy("sort", true)
							.orderBy("create_time", false)
		);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				JazProdTypeDO obj = (JazProdTypeDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazProdType:add")
	String add(){
	    return "jzweb/jazProdType/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazProdType:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazProdTypeDO jazProdType = jazProdTypeService.selectById(id);
		model.addAttribute("jazProdType", jazProdType);
	    return "jzweb/jazProdType/edit";
	}
	
	@Log(value = "添加商品类型表",sysModule = 6,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazProdType:add")
	public Result<String> save( JazProdTypeDO jazProdType){
		jazProdTypeService.insert(jazProdType);
        return Result.ok();
	}
	
	@Log(value = "修改商品类型表",sysModule = 6,operationType = 3)
	@ResponseBody
	@PostMapping("/update")
//	@RequiresPermissions("jzweb:jazProdType:edit")
	public Result<String>  update(@RequestBody JazProdTypeDO jazProdType){
		boolean update = jazProdTypeService.updateById(jazProdType);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "删除商品类型表",sysModule = 6,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("jzweb:jazProdType:remove")
	public Result<String>  remove( Long id){
		jazProdTypeService.deleteById(id);
        return Result.ok();
	}
	
	@Log(value = "批量删除商品类型表",sysModule = 6,operationType = 2)
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazProdType:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazProdTypeService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@Log(value = "添加商品类型",sysModule = 6,operationType = 1)
	@ResponseBody
	@PostMapping("/saveProdType")
//	@RequiresPermissions("jzweb:jazGroupProd:saveGroupProd")
	public Result<String> saveProdType(@RequestBody JazProdTypeDO jazProdTypeDO){
		jazProdTypeService.saveProdType(jazProdTypeDO);
		return Result.ok();
	}


	@ResponseBody
	@GetMapping("/selProdType")
//	@RequiresPermissions("jzweb:jazGroupProd:saveGroupProd")
	public Result<List<JazProdTypeDO>> selProdType() {
		return Result.ok(jazProdTypeService.selectList(new EntityWrapper<JazProdTypeDO>().eq("status",1)));
	}


	@ResponseBody
	@GetMapping("/selProdTypePage")
//	@RequiresPermissions("jzweb:jazGroupProd:saveGroupProd")
	public Result<Page<JazProdTypeDO>> selProdTypePage(@RequestParam(value = "name",required = false) String name,
													   @RequestParam("current")int current,
													   @RequestParam("size")int size) {
		return jazProdTypeService.selProdTypePage(name,current,size);
	}

}
